<?php 
    echo $this->Html->css('grid.inclusao');
    echo $this->Html->css('matricula');
    
    echo $this->Html->script('grid.inclusao');
    echo $this->Html->script('matricula');
?>

<script type="text/javascript">
    $(function(){
        
        $("#unidadeEnsino").change(function(){
            if($(this).val()!=''){
                $('#curso').html('<option value="">Atualizando...</option>');
                
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: '<?php echo $this->webroot . 'ajax/atualizar_cursos_turma/'; ?>'+ $(this).val(),
                    success: function(data){
                        $('#curso').html(data);
                    }
                });
            }else{
                $('#curso').html('<option value="">--Selecione--</option>');
            }
        });
        
        $("#curso").change(function(){
            if($(this).val()!=''){
                $('#turma').html('<option value="">Atualizando...</option>');
                
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: '<?php echo $this->webroot . 'ajax/atualizar_turma_por_unidade_curso/'; ?>'+ $("#unidadeEnsino").val() + '/' + $(this).val(),
                    success: function(data){
                        $('#turma').html(data);
                    }
                });
            }else{
                $('#turma').html('<option value="">--Selecione--</option>');
            }
        });
        
        $('#turma').change(function(){
            limparDisciplinas();
            
            if($(this).val()!=''){
                $.ajax({
                    cache: false,
                    type: "GET",
                    url: '<?php echo $this->webroot . 'ajax/obter_disciplinas_por_turma/'; ?>' + $(this).val(),
                    success: function(data){
                        $('#gridDisciplinas').append(data);
                    }
                });
            }    
        });
        
        $("#btnAddDetalhamento").click(function(){
            $("input:checked", "#gridDisciplinas").each(function(){
                var template = $("#xmpDetalhamento").html();
                template = template.replace(/#idd#/g, indice++);
                var $template = $(template);
                
                $("#gridDetalhamento").append($template);
            
                $("#turmaId" + (indice - 1), "#gridDetalhamento").val($("option:selected", "#turma").val());
                $("#turmaNome" + (indice - 1), "#gridDetalhamento").val($("option:selected", "#turma").text());
                
                var ind = $(this).attr("indice");
                var linha = $(this).parent().parent();
                
                $("#disciplinaId" + (indice - 1), "#gridDetalhamento").val($(this).val());
                $("#disciplinaNome" + (indice - 1), "#gridDetalhamento").val($("#codigoDis" + ind, linha).val() + " - " + $("#nomeDis" + ind, linha).val());
                $("#valorDisciplinaHidden" + (indice - 1), "#gridDetalhamento").val($("#valorDisCur" + ind, linha).val());
                $("#valorDisciplina" + (indice - 1), "#gridDetalhamento").val($("#valorDisCur" + ind, linha).val());
                
                $(".btnGridDel").click(function() {
                    var ref = $(this).attr("ref");
                    var ind = $(this).attr("indice");

                    $("#btnGridDel" + ref + ind).parents("tr").remove();
                    
                    atualizarValorTotal();
                });
            });
            
            atualizarValorTotal();
        });
        
        $(".btnGridAdd").click(function(){
            $('.money').mask("#.##0,00", {reverse: true, maxlength: false});
            atualizacaoValorParcelas();
            
            $(".btnGridDel").click(function(){
                atualizarValorTotalPagto();
            });
        });
        
         $(".btnGridDel").click(function() {
            var ref = $(this).attr("ref");
            var ind = $(this).attr("indice");

            $("#btnGridDel" + ref + ind).parents("tr").remove();
                    
            atualizarValorTotal();
         });
    
    });
    
</script>

<div class="matriculas form">
<?php echo $this->Form->create('Matricula'); ?>
	<fieldset>
		<legend><?php echo __('Editar Matricula'); ?></legend>
	<?php
		echo $this->Form->input('id');
                echo $this->Form->input('numero', array('label' => 'Número', 'disabled' => true));
                echo $this->Form->input('Aluno.Pessoa.nome', array('label' => 'Aluno', 'type' => 'text', 'disabled' => true));
		echo $this->Form->input('UnidadeEnsino.id', array('label' => 'Unidade de Ensino', 'id' => 'unidadeEnsino','options' => $unidadesEnsino));
                echo $this->Form->input('Curso.id', array('label' => 'Curso', 'id' => 'curso','options' => $cursos));
		echo $this->Form->input('data', array('type' => 'text', 'class' => 'datepicker'));
                echo $this->Form->input('valor', array('type' => 'text', 'label' => 'Valor da Matrícula', 'id' => 'valorMatricula', 'class' => 'money'));
	?>
                
        
            <br />
            <h3><?php echo __('Disciplinas'); ?></h3>
            
            <?php echo $this->Form->input('turma_id', array('id' => 'turma')); ?>
            
            <table id="gridDisciplinas" cellpadding = "0" cellspacing = "0">
                <tr>
                    <th><?php echo __('Disciplinas'); ?></th>
                    <th><?php echo __('Selecionar'); ?></th>
                </tr>
            </table>
            
            <div>
                <?php echo $this->form->button('Incluir', array('type' => 'button', 'id' => 'btnAddDetalhamento', 'class' => 'btnAddDetalhamento')); ?>
            </div>
        
            <br /><br />
            <h3><?php echo __('Detalhamento'); ?></h3>
            <table id="gridDetalhamento" cellpadding = "0" cellspacing = "0">
                <tr>
                    <th><?php echo __('Turma'); ?></th>
                    <th><?php echo __('Disciplina'); ?></th>
                    <th><?php echo __('Valor'); ?></th>
                    <th class="actions"><?php echo __('Ações'); ?></th>
                </tr>
                <?php $linha = 0;
                      foreach($this->request->data['TurmaMatricula'] as $chaveTM => $valorTM){
                        foreach($valorTM['DisciplinaTurmaMatricula'] as $chaveDTM => $valorDTM){?>
                
                            <script type="text/javascript">indice = <?php echo $linha;?></script>
                
                            <tr>
                                <td>
                                    <?php
                                        echo $this->Form->hidden('TurmaMatricula.'. $linha .'.id', array('id' => 'turmaMatriculaId'.$linha, 'value' => $valorTM['id']));
                                        echo $this->Form->hidden('TurmaMatricula.'. $linha .'.turma_id', array('id' => 'turmaId'.$linha, 'value' => $valorTM['turma_id']));
                                        echo $this->Form->input('TurmaMatricula.'. $linha .'.turma_nome', array('type' => 'text', 'value' => $valorTM['Turma']['codigo'], 'label' => false, 'id' => 'turmaNome'.$linha, 'disabled' => true)); 
                                    ?>
                                </td>
                                <td>
                                    <?php 
                                        echo $this->Form->hidden('TurmaMatricula.'. $linha .'.DisciplinaTurmaMatricula.id', array('id' => 'DisciplinaTurmaMatriculaId'.$linha, 'value' => $valorDTM['id']));
                                        echo $this->Form->hidden('TurmaMatricula.'. $linha .'.DisciplinaTurmaMatricula.disciplina_id', array('id' => 'disciplinaId'.$linha, 'value' => $valorDTM['disciplina_id']));
                                        echo $this->Form->input('TurmaMatricula.'. $linha .'.DisciplinaTurmaMatricula.disciplina_nome', array('type' => 'text', 'value' => $valorDTM['Disciplina']['codigo']. ' - ' .$valorDTM['Disciplina']['nome'], 'label' => false, 'id' => 'disciplinaNome'.$linha, 'disabled' => true)); 
                                    ?>
                                </td>
                                <td>
                                    <?php 
                                        echo $this->Form->hidden('TurmaMatricula.'. $linha .'.DisciplinaTurmaMatricula.valor', array('id' => 'valorDisciplinaHidden'.$linha, 'value' => $valorDTM['valor']));
                                        echo $this->Form->input('TurmaMatricula.'. $linha .'.DisciplinaTurmaMatricula.valor', array('type' => 'text', 'label' => false, 'value' => $valorDTM['valor'], 'id' => 'valorDisciplina'.$linha, 'class' => 'money money_grid valorDisciplina', 'disabled' => true)); 
                                    ?>
                                </td>
                                <td class="actions">
                                    <?php echo $this->form->button('Excluir', array('type' => 'button', 'id' => 'btnGridDelDetalhamento'.$linha, 'class' => 'btnGridDel', 'ref' => 'Detalhamento', 'indice' => $linha))?>
                                </td>
                            </tr>
                <?php 
                            $linha++;
                        }
                      }
                ?>
            </table>
            
            <table>
                <tr>
                    <td id="tdLbValorTotal">Valor Total</td>
                    <td id="tdValorTotal">
                        <?php
                            echo $this->Form->input('Controle.valor_total', array('type' => 'text', 'value' => $this->Formatter->formatarDecimal($valorTotal), 'label' => false, 'id' => 'valorTotal', 'div' => false, 'disabled')); 
                        ?>
                    </td>
                </tr>
            </table>
            
            <table>
                <tr>
                    <td id="tdLbValorTotal">Total Geral</td>
                    <td id="tdValorTotal">
                        <?php
                            echo $this->Form->input('Controle.total_geral', array('type' => 'text', 'value' => $this->Formatter->formatarDecimal($totalGeral), 'label' => false, 'id' => 'totalGeral', 'div' => false, 'disabled')); 
                        ?>
                    </td>
                </tr>
            </table>
        
            <br /><br />
            <h3><?php echo __('Forma de Pagamento'); ?></h3>
            <div>
                <?php echo $this->form->button('Incluir', array('type' => 'button', 'ref' => 'FormaPagamento', 'class' => 'btnGridAdd')); ?>
            </div>

            <table id="gridFormaPagamento" cellpadding = "0" cellspacing = "0">
                <tr>
                    <th><?php echo __('Tipo'); ?></th>
                    <th><?php echo __('Valor'); ?></th>
                    <th><?php echo __('Parcelas'); ?></th>
                    <th><?php echo __('Valor Parcela'); ?></th>
                    <th class="actions"><?php echo __('Ações'); ?></th>
                </tr>
                <?php foreach ($this->request->data['FormaPagamentoMatricula'] as $chave => $valor){?>
                
                    <script type="text/javascript">indice++</script>
                    
                    <tr>
                        <td>
                            <?php echo $this->Form->hidden('FormaPagamentoMatricula.'.$chave.'.id', array('value' => $valor['id']));?>
                            <?php echo $this->Form->input('FormaPagamentoMatricula.'.$chave.'.forma_pagamento_id', array('label' => false, 'value' => $valor['forma_pagamento_id'], 'options' => $formasPagamento)); ?>
                        </td>
                        <td>
                            <?php echo $this->Form->input('FormaPagamentoMatricula.'.$chave.'.valor', array('type' => 'text', 'value' => $valor['valor'], 'label' => false, 'id' => 'valorPagto'.$chave, 'class' => 'money money_grid valorPagto', 'indice' => $chave)); ?>
                        </td>
                        <td>
                            <?php echo $this->Form->input('FormaPagamentoMatricula.'.$chave.'.parcelas', array('label' => false, 'value' => $valor['parcelas'], 'id' => 'parcPagto'.$chave, 'class' => 'qtdParcelas parcPagto', 'indice' => $chave, 'required' => false)); ?>
                        </td>
                        <td>
                            <?php echo $this->Form->input('FormaPagamentoMatricula.'.$chave.'.valor_parcela', array('type' => 'text', 'value' => $valor['valor_parcela'], 'label' => false, 'id' => 'vlParcela'.$chave, 'class' => 'money money_grid', 'required' => false)); ?>
                        </td>
                        <td class="actions">
                            <?php echo $this->form->button('Excluir', array('type' => 'button', 'id' => 'btnGridDelFormaPagamento'.$chave, 'class' => 'btnGridDel', 'ref' => 'FormaPagamento', 'indice' => $chave)) ?>
                        </td>
                    </tr>
                <?php } ?>
            </table>
            
            <table>
                <tr>
                    <td id="tdLbValorTotal">Valor Total</td>
                    <td id="tdValorTotal">
                        <?php
                            echo $this->Form->input('Controle.valor_total_pagamento', array('type' => 'text', 'value' => $this->Formatter->formatarDecimal($valorTotalPagto), 'label' => false, 'id' => 'valorTotalPagto', 'div' => false, 'disabled')); 
                        ?>
                    </td>
                </tr>
            </table>
                
	</fieldset>
    
        
<?php echo $this->Form->end(__('Salvar')); ?>
</div>
<div class="actions">
	<h3><?php echo __('Ações'); ?></h3>
	<ul>

		<li><?php echo $this->Form->postLink(__('Excluir'), array('action' => 'delete', $this->Form->value('Matricula.id')), array(), __('Confirma exclusão da matrícula %s?', $this->Form->value('Matricula.numero'))); ?></li>
		<li><?php echo $this->Html->link(__('Listar Matriculas'), array('action' => 'index')); ?></li>
		<li><?php echo $this->Html->link(__('Listar Turmas'), array('controller' => 'turmas', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('Nova Turma'), array('controller' => 'turmas', 'action' => 'add')); ?> </li>
		<li><?php echo $this->Html->link(__('Listar Alunos'), array('controller' => 'alunos', 'action' => 'index')); ?> </li>
		<li><?php echo $this->Html->link(__('Novo Aluno'), array('controller' => 'alunos', 'action' => 'add')); ?> </li>
	</ul>
</div>

<xmp style="display:none" id="xmpDetalhamento">
    <tr>
        <td>
            <?php
                echo $this->Form->hidden('TurmaMatricula.#idd#.turma_id', array('id' => 'turmaId#idd#'));
                echo $this->Form->input('TurmaMatricula.#idd#.turma_nome', array('type' => 'text', 'label' => false, 'id' => 'turmaNome#idd#', 'disabled' => true)); 
            ?>
        </td>
        <td>
            <?php 
                echo $this->Form->hidden('TurmaMatricula.#idd#.DisciplinaTurmaMatricula.disciplina_id', array('id' => 'disciplinaId#idd#'));
                echo $this->Form->input('TurmaMatricula.#idd#.DisciplinaTurmaMatricula.disciplina_nome', array('type' => 'text', 'label' => false, 'id' => 'disciplinaNome#idd#', 'disabled' => true)); 
            ?>
        </td>
        <td>
            <?php 
                echo $this->Form->hidden('TurmaMatricula.#idd#.DisciplinaTurmaMatricula.valor', array('id' => 'valorDisciplinaHidden#idd#'));
                echo $this->Form->input('TurmaMatricula.#idd#.DisciplinaTurmaMatricula.valor', array('type' => 'text', 'label' => false, 'id' => 'valorDisciplina#idd#', 'class' => 'money money_grid valorDisciplina', 'disabled' => true)); 
            ?>
        </td>
        <td class="actions">
            <?php echo $this->form->button('Excluir', array('type' => 'button', 'id' => 'btnGridDelDetalhamento#idd#', 'class' => 'btnGridDel', 'ref' => 'Detalhamento', 'indice' => '#idd#'))?>
        </td>
    </tr>
</xmp>

<xmp style="display:none" id="xmpFormaPagamento">
    <tr>
         <td>
            <?php echo $this->Form->input('FormaPagamentoMatricula.#idd#.forma_pagamento_id', array('label' => false, 'options' => $formasPagamento)); ?>
        </td>
        <td>
            <?php echo $this->Form->input('FormaPagamentoMatricula.#idd#.valor', array('type' => 'text', 'label' => false, 'id' => 'valorPagto#idd#', 'class' => 'money money_grid valorPagto', 'indice' => '#idd#')); ?>
        </td>
        <td>
            <?php echo $this->Form->input('FormaPagamentoMatricula.#idd#.parcelas', array('label' => false, 'id' => 'parcPagto#idd#', 'class' => 'qtdParcelas parcPagto', 'indice' => '#idd#', 'required' => false)); ?>
        </td>
        <td>
            <?php echo $this->Form->input('FormaPagamentoMatricula.#idd#.valor_parcela', array('type' => 'text', 'label' => false, 'id' => 'vlParcela#idd#', 'class' => 'money money_grid', 'required' => false)); ?>
        </td>
        <td class="actions">
            <?php echo $this->form->button('Excluir', array('type' => 'button', 'id' => 'btnGridDelFormaPagamento#idd#', 'class' => 'btnGridDel', 'ref' => 'FormaPagamento', 'indice' => '#idd#'))?>
        </td>
    </tr>
</xmp>
